﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;

namespace PFAGame
{
    class SoundManager
    {
        List<SoundEffect> sound_effects;
        List<SoundEffect> musics;
        List<SoundEffect> voices;

        SoundEffectInstance BGM;
        SoundEffectInstance SV;

        public SoundManager()
        {
            singleton = this;
            LoadMusics();
        }

        #region data

        const string ext = ".mp3";

        string[] musicsName = new string[]
        {
            "Musics/Cave_loop",
            "Musics/Desert_loop",
            "Musics/Factory_loop",
            "Musics/Menu_loop",
            "Musics/Town_loop",
            "Musics/Game_over_one_shot",
            "Musics/Victory_one_shot"
        };

        string[] voicesName = new string[]
        {
            "Voices/guide_1",
            "Voices/cavalier_1",
            "Voices/cavalier_2",
            "Voices/fou_1",
            "Voices/fou_2",
            "Voices/tours_1",
            "Voices/tours_2",
            "Voices/guide_7",
        };

        #endregion

        void LoadMusics()
        {
            sound_effects = new List<SoundEffect>();
            musics = new List<SoundEffect>();
            voices = new List<SoundEffect>();

            foreach (string str in musicsName)
                musics.Add(ScreenManager.Singleton.Game.Content.Load<SoundEffect>(str));
            foreach (string str in voicesName)
                voices.Add(ScreenManager.Singleton.Game.Content.Load<SoundEffect>(str)); 
        }

        public void PlayVoice(int i)
        {
            try
            {
                if (SV != null)
                {
                    SV.Stop();
                    SV.Dispose();
                }
                SV = voices[i].CreateInstance();
                SV.Play();
                SV.Volume = 0.3f;
                SV.IsLooped = false;
            }
            catch { };
        }

        public void PlayMusic(int i)
        {
            try
            {
                if (BGM != null)
                {
                    BGM.Stop();
                    BGM.Dispose();
                }
                BGM = musics[i].CreateInstance();
                BGM.Play();
                BGM.Volume = 0.2f;
                BGM.IsLooped = true;
            }
            catch { };
        }

        public static SoundManager Singleton
        {
            get
            {
                return singleton;
            }
        }
        private static SoundManager singleton;
    }
}
